'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:SignIndexCtrl
 * @description
 * # SignIndexCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('SignIndexCtrl', function ($scope, UserInfo, $location, localStorageService, MapService, Util, DataService, openApi, FileService) {
    var vm = this;
    vm.param = {};
    vm.addpic = function () {
      if (vm.param.attachment) {
        Util.Toast("只能上传一张图片")
        return
      }
      choosePic();
    }
    vm.delpic = function () {
      FileService.delfile(vm.param.attachment).then(function (data) {
        vm.param.attachment = "";
      });
    };
    vm.goDetail = function () {
      $scope.src = openApi.showimg + "?fileName=" + vm.param.attachment;
      Util.showFile($scope);
    };
    $scope.closefile = Util.hideFile;
    var choosePic = function () {
      var btn = 1;//只能拍照
      var cameraOptions = {
        quality: 30,
        EncodingType: 0,
        destinationType: 1
      };
      cameraOptions.sourceType = btn
      navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);
    }
    var cameraSuccess = function (imageData) {
      FileService.upload(imageData, openApi.uploadFile).then(function (data) {
        vm.param.attachment = data;
        $scope.$apply();
      });
    }
    var cameraError = function () {
    };
    vm.getPosition = function () {
      vm.param.address = "";
      vm.param.title = "";
      vm.positiontip = "定位中.."
      MapService.getcurPositon().then(function (data) {
        vm.param.point = {};
        vm.param.point.lat = data.latitude;
        vm.param.point.lng = data.longitude;
        MapService.getLocation(data).then(function (data) {
          vm.param.address = data;
          vm.positiontip = "";
        });
      }, function (data) {
        Util.Toast(data);
        vm.positiontip = "定位失败，请重新定位";
      });
    };
    // 打卡
    vm.sign = function () {
      var param = {};
      if (!vm.param.point) {
        Util.Toast("请获取当前位置信息")
        return
      }
      param.lat = vm.param.point.lat
      param.lng = vm.param.point.lng
      param.title = vm.param.title;
      param.address = vm.param.address;
      param.remark = vm.param.remark;
      param.attachment = vm.param.attachment;
      DataService.getData(openApi.signin, param).then(function (data) {
        console.log(data);
        if (data.code == 1) {
          Util.Toast(data.result, { callback: Util.goBack })
        } else {
          Util.Toast(data.result)
        }
      })
    };
    vm.goFootmark = function () {
      $location.path("sign/footmark").replace()
    }
    vm.chooseposition = function () {
      localStorageService.set("signparam", vm.param)
      $location.path("sign/chooselocation")
    }
    // 获取位置
    var getPosition = function () {
      vm.param = localStorageService.get("signparam");
      localStorageService.remove("signparam");
      if (!vm.param) {
        vm.param = {}
        vm.getPosition();
      }
    }
    // 获取当前考勤信息
    var getSigndata = function () {
      DataService.getData(openApi.signinStatus).then(function (data) {
        vm.curData = data;
      })
    }
    var init = function () {
      getPosition();
      getSigndata();
      UserInfo.getUseinfo().then(function (data) {
        vm.UserInfo = data;
      });
    }
    init();
  });
